package controlleur;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import json.JsonParserSuccursale;
import messages.SystemMessage;
import util.HOST;
import data.Succursale;

public class succursaleControlleur {

	public ArrayList<Succursale> getSuccursaleFromDetaillant(int idDet)
	{
		ArrayList<Succursale> succursales = new ArrayList<Succursale>();		

		String strUrl  =("http://"+HOST.HOST+"/proxirabaisServer/SuccursaleServlet?"+SystemMessage.ACTION+"="+SystemMessage.GET_ALL_SUCCUR+"&"+SystemMessage.DETAILLANT_ID+"="+idDet);
		//System.out.println(strUrl);
		String str;

		try {

			URL url = new URL(strUrl);
			JsonParserSuccursale jsonParsSucc = new JsonParserSuccursale();

			//lire le text recu du serveur
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));			
			while ((str = in.readLine()) != null) {
				Succursale s = jsonParsSucc.jsonRead(str);
				succursales.add(s);
			}			
			in.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}		
		return succursales;
	}
	
	public Succursale getSuccursale(int idSucc)
	{		
		String strUrl  =("http://"+HOST.HOST+"/proxirabaisServer/SuccursaleServlet?"+SystemMessage.ACTION+"="+SystemMessage.GET_SUCCUR+"&"+SystemMessage.SUCCURSALE_ID+"="+idSucc);
		String str;
		Succursale s = new Succursale();
		
		try {
			URL url = new URL(strUrl);
			JsonParserSuccursale jsonParsSucc = new JsonParserSuccursale();

			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));			
			if ((str = in.readLine()) != null) {
				s = jsonParsSucc.jsonRead(str);				
			}			
			in.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}		
		return s;
	}
}
